#ifndef XTEXTURE_H
#define XTEXTURE_H
#include "Common.h"
#include "DirectXTK\SpriteBatch.h"
#include "DirectXTK\DDSTextureLoader.h"
#include <d3d11_1.h>
#include <vector>
class Texture
{
	friend class TextureManager;
	char* name;
	UINT width, height;
	int mipmap;
	//D3D11_TEXTURE_ADDRESS_MODE wrapMode; Move to sampler
	//D3D11_FILTER filter; Move to sampler
public:
	ID3D11ShaderResourceView* shaderResourceView;
	ID3D11Resource* resource;
	Texture()
	{
		//wrapMode = D3D11_TEXTURE_ADDRESS_WRAP;
		//filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
		mipmap = 0;
	}
	virtual ~Texture()
	{
		SafeRelease(shaderResourceView);
		SafeRelease(resource);
		SafeDeleteArray(name);
	}
	UINT GetWidth() {return width;}
	UINT GetHeight() {return height;}
	/*
	D3D11_TEXTURE_ADDRESS_MODE GetWrapMode() {return wrapMode;}
	D3D11_FILTER GetFilter() {return filter;}
	void SetWrapMode(const D3D11_TEXTURE_ADDRESS_MODE &mode)
	{
		wrapMode = mode;
	}
	void SetFilter(const D3D11_FILTER &mode)
	{
		filter = mode;
	}
	*/
	const char* GetName()
	{
		return name;
	}
	bool HasMipmap()
	{
		return mipmap > 0;
	}
};
class TextureManager : public Singleton<TextureManager>
{
	std::vector<Texture*> textureList;
	char* directory;
public:
	static void Init(char* directory = NULL)
	{
		SafeDelete(instance);
		instance = new TextureManager();
		if(directory != NULL)
		{
			SafeCopyCHAR(&instance->directory, directory);
		}
		else
		{
			instance->directory = new char[1];
			instance->directory[0] = 0;
		}
	}
	static void Release()
	{
		SafeDelete(instance);
	}
	~TextureManager()
	{
		for(unsigned int i = 0;i<textureList.size();i++)
		{
			SafeDelete(textureList[i]);
		}
		textureList.clear();
		SafeDeleteArray(directory);
	}
	Texture* LoadTexture(const char* name);
	Texture* GetTexture(const char* name);
	Texture* CreateTexture(const char* name, int width, int height, DXGI_FORMAT format, UINT bindFlags, int aa = 0);
	void ReleaseTexure(Texture* texture);
	void ReleaseTexure(const char* name);
	void SetDirectory(char* directory)
	{
		SafeCopyCHAR(&this->directory, directory);
	}
};
void GetTextureSize( ID3D11Resource* res, UINT* width, UINT* height );
#endif